import { defineStore } from 'pinia'
import {getUnReadMsg,sendInvitation} from '../../api/team/team'


const useTeamStore = defineStore('team', {
  state: () => {
    return { 
		teamUpdateID: {},
    invitationMsgList:[]

	 }
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    getTeamUpdateID(val) {
      this.teamUpdateID = val
    },
    //消息
    getInvitationMsg(){
      getUnReadMsg().then(res=>{
        console.log("res:",res);
        this.invitationMsgList=res.data
        const unReadMsgList=res.data.filter(item=>item.isRead===false)
        //如果未读消息为空 隐藏红点
        if(unReadMsgList.length<=0){
          uni.hideTabBarRedDot({
            index:2
          })
          
        }else{

        //否则设置红点
          uni.showTabBarRedDot({
            index:2
          })
        }
      })
    },
    /**
     * 
     * @param {*} sendMsgObj 组队请求消息发送对象
     */
    sendInvitation:async (sendMsgObj)=>{
      const res=await sendInvitation(sendMsgObj)
      console.log("组队请求结果："+res);
      try {
        
        
        return Promise.resolve(res)
      } catch (error) {
        console.log("捕捉到错误："+error);
        
        return Promise.reject(error)
      }
    }
  },
  getters: {
    TEAMUPDATEID: (state) => state.teamUpdateID,
    INVITATIONMSGLIST:(state)=>state.invitationMsgList
  },
})
export default useTeamStore
